<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['ispay'])){
		$params['ispay']=intval($_RQ['ispay']);
		$return['params']['ispay']=$_RQ['ispay'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['dian_id'])){
		$params['api_data like']='%area_dianid":"'.intval($_RQ['dian_id']).'%';
		$return['params']['dian_id']=$_RQ['dian_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','ordersn','address','realname','mobile','mid','paytype','api_data','createtime');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$address=json_decode($item['address'],true);
			$realname=$address['name']?:$item['realname'];
			$mobile=$address['mobile']?:$item['mobile'];
			$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id']),array('price','total','goodsid'));
			$item['total']=0;
			$item['api_data']=json_decode($item['api_data'],true);
			foreach($goods as &$v){
				$item['total'] +=$v['total'];
				$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image'));
				$return['list'][]=array(
					'ordersn'=>$item['ordersn'],
					'createtime'=>date('Y-m-d H:i:s',$item['createtime']),
					'image'=>$good['image'],
					'title'=>$good['title'],
					'price'=>$v['price'],
					'total'=>$v['total'],
					'dian_id'=>$item['api_data']['area_dianid'],
					'realname'=>$realname,
					'mobile'=>$mobile,
				);
			}
		}
	}
	//$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'total'=>$total,'psize'=>$psize,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'dian_id','title'=>'自提点','type'=>'select2','options'=>'dians'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'price','title'=>'单价','type'=>'string'),
			array('name'=>'total','title'=>'商品数量','type'=>'string'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'string'),
		)),'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'dian_id','width'=>10,'title'=>'自提点','type'=>'select','options'=>'dians'),
			array('name'=>'ispay','width'=>10,'title'=>'支付状态','type'=>'radio','options'=>'ispay'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>10,'title'=>'配送方式_Ship type','type'=>'radio','options'=>'sendtype'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	$return['dians']=$this->getall('agent',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['ispay']=array('未支付','已支付');
	$return['buytype']=$this->buy_types;
	$return['sendtype']=$this->send_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['ispay'])){
		$params['ispay']=intval($_RQ['ispay']);
		$return['params']['ispay']=$_RQ['ispay'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['dian_id'])){
		$params['api_data like']='%area_dianid":"'.intval($_RQ['dian_id']).'%';
		$return['params']['dian_id']=$_RQ['dian_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$fields=array('id','ordersn','address','realname','mobile','mid','paytype','api_data','createtime');
	$order=pdo_getall('jy_weishop_order',$params,$fields);
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$ordergoods=array();
	if($order){
		foreach($order as $item){
			$address=json_decode($item['address'],true);
			$realname=$address['name']?:$item['realname'];
			$mobile=$address['mobile']?:$item['mobile'];
			$order_goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),array('price','total','goodsid'));
			$item['total']=0;
			$item['api_data']=json_decode($item['api_data'],true);
			foreach($order_goods as $v){
				$item['total'] +=$v['total'];
				$good=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['goodsid']),array('title','image'));
				$diantitle=pdo_getcolumn('jy_area_dian_agent',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['api_data']['area_dianid']),'title');
				$ordergoods[]=array(
					'ordersn'=>$item['ordersn'],
					'createtime'=>date('Y-m-d H:i:s',$item['createtime']),
					'image'=>$good['image'],
					'title'=>$good['title'],
					'price'=>$v['price'],
					'total'=>$v['total'],
					'dian_id'=>$diantitle,
					'realname'=>$realname,
					'mobile'=>$mobile,
				);
			}
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'diantitle'=>'自提点',
		'title'=>'商品',
		'price'=>'金额',
		'total'=>'数量',
		'realname'=>'收货人姓名',
		'mobile'=>'收货人电话',
		'createtime'=>'下单时间',
	);
	cfc('tools')->export2excel($header,$ordergoods,'商城订单列表');
}